<?php 
namespace app\index\controller;

use app\index\model\User;
use think\Controller;
use think\Request;

header("content-type:text/html;charset=utf-8");
class Index extends Controller
{
    // 表单页面
    public function index()
    {
        return view();
    }

    // 用户注册
    public function insert()
    {
        $request = Request::instance();
        $data = $request->post();
        // var_dump($data);
        if($data)
        {
            $user = new User;
            $sql = $user->insertData($data);
            if($sql)
            {
                // return Redirect('Index/show');
                $this->success('注册成功！','Index/show');
            }
            else
            {
                // echo "注册失败！";
                $this->error('注册失败！');
            }
        }
        else
        {
            // echo "数据提交有误！";
            $this->error('数据提交有误！');
        }
    }

    // 用户列表
    public function show()
    {
        $user = new User;
        $arr = $user->show();
        return view('show',['arr'=>$arr]);
    }

    // 用户删除
    public function delete()
    {
        $Request = Request::instance();
        $id = $Request->get('id');
        // var_dump($get);die;
        $user = new User;
        $result = $user->deleteData($id);
        if($result)
        {
            // return Redirect('Index/show');
            $this->success('删除成功！','Index/show');
        }
        else
        {
            // echo "删除失败！";
            $this->error('删除失败！');
        }

    }

    public function Welcome()
    {
        return view();
    }

     //修改页面
   public function update()
   {
        $Request=Request::instance();
        $id=$Request->get('id');
        $user=new User;
        $res=$user->findData($id);//查询单条
        //print_r($res);die;
        return view('update',['res'=>$res]);
   }

   //执行修改
   public function save()
   {
        $Request=Request::instance();
        $id=$Request->post('id');
        $Request=Request::instance();
        $data=$Request->post();//接修改之后的数据
        $user=new User;
        $result=$user->updateData($data,$id);//修该语句
        if($result)
        {
            return Redirect('Index/show');
        }else
        {
            $this->error('修改失败');
        }

    }



}


 ?>